package com.xw.cor.servelt;

import com.sun.net.httpserver.Filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xw
 * @create: 2022/10/18 9:50
 * @description: 责任链模式
 */
public class Servlet_Main {

    public static void main(String[] args) {
        Request request = new Request();
        request.str = "大家好:)，<script>，欢迎访问 mashibing.com ，大家都是996 ";

        Response response = new Response();
        response.str = "你才996,<***>,";
        HTMLFilter htmlFilter = new HTMLFilter();
//        htmlFilter.doFilter(request,response);
        FilterChain filterChain = new FilterChain();
        filterChain.add(htmlFilter).add(new SensitiveFilter());
        filterChain.doFilter(request,response);

    }

    interface Filter {
        boolean doFilter(Request request,Response response);
    }

    static class Request {
        String str;
    }

    static class Response {
        String str;
    }

    static class HTMLFilter implements  Filter{
        @Override
        public boolean doFilter(Request request, Response response) {
            request.str = request.str.replaceAll("<", "[").replaceAll(">", "]");
            response.str = response.str.replaceAll("<", "M").replaceAll(">", "M");
            return true;
        }
    }

    static class SensitiveFilter implements Filter{
        @Override
        public boolean doFilter(Request request, Response response) {
            request.str = request.str.replaceAll("996", "955");
            response.str = response.str.replaceAll("996", "007");
            return true;
        }
    }
    static class FilterChain implements Filter{
        List<Filter> filters = new ArrayList<>();
        public FilterChain add(Filter f){
            filters.add(f);
            return this;
        }

        @Override
        public boolean doFilter(Request request, Response response) {
            for(Filter f : filters ){
                f.doFilter(request, response);
            }
            return false;
        }
    }


}
